package com.test.mylotterysystem.comment.aspect;

import com.test.mylotterysystem.comment.Pojo.CommentResult;
import com.test.mylotterysystem.comment.errorCode.GlobalErrorCode;
import com.test.mylotterysystem.comment.errorCode.ServiceErrorCode;
import com.test.mylotterysystem.comment.exception.ControllerException;
import com.test.mylotterysystem.comment.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import static com.test.mylotterysystem.comment.errorCode.GlobalErrorCode.PARAM_IS_ERROR;

@RestControllerAdvice
@Slf4j
public class ExceptionAspect {

    @ExceptionHandler(value = ControllerException.class)
    public CommentResult<?> handler(ControllerException controllerException) {
        log.error("发生Controller层的异常，" + controllerException.getErrorMsg());
        return CommentResult.fail(controllerException.getCode(), controllerException.getErrorMsg());
    }

    @ExceptionHandler(value = ServiceException.class)
    public CommentResult<?> handler(ServiceException serviceException) {
        log.error("发生Service层的异常，" + serviceException.getErrorMsg());
        return CommentResult.fail(serviceException.getCode(), serviceException.getErrorMsg());
    }

    @ExceptionHandler(value = Exception.class)
    public CommentResult<?> handler(Exception exception) {
        log.error("发生exception层的异常，" + exception.getMessage());
        return CommentResult.fail(GlobalErrorCode.INTERNAL_SERVER_ERROR.getCode(), exception.getMessage());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public CommentResult<?> handler(MethodArgumentNotValidException methodArgumentNotValidException){
        log.error("参数传递有误，"+methodArgumentNotValidException.getMessage());
        return CommentResult.fail(PARAM_IS_ERROR.getCode(),PARAM_IS_ERROR.getErrorMsg());
    }
}
